package com.edu.service.impl;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import com.edu.dao.BaseDao;
import com.edu.dao.BookDao;
import com.edu.dao.impl.BookAndUserDaoImpl;
import com.edu.dao.impl.BookDaoImpl;
import com.edu.entiy.Book;
import com.edu.entiy.History;
import com.edu.entiy.Page;
import com.edu.entiy.User;
import com.edu.service.BookService;
import com.edu.utils.DateUtils;
import com.edu.utils.PageTool;

public class BookServiceImpl implements BookService {
	private BookDao bookDao = new BookDaoImpl();
	@Override
	public Page<Book> selectByPage(int currentPage, int pageSize) {
		// TODO Auto-generated method stub
		Page<Book> page = new Page<>();
		int count = bookDao.selectBookCount();
		page.setPageCount(count);
		int begin = (currentPage-1)*pageSize;
		List<Book> bookTypes = bookDao.selectPageBook(begin, pageSize);
		page.setPageData(bookTypes);
		return page;
	}
	@Override
	public Page<Book> selectByPage(Book book, int currentPage, int pageSize) {
		// TODO Auto-generated method stub
		Page<Book> page = new Page<>();
		int count = bookDao.selectBookCountByCondition(book);
		page.setPageCount(count);
		int begin = (currentPage-1)*pageSize;
		List<Book> bookTypes = bookDao.selelectBookPageByCondition(book, begin, pageSize);
		page.setPageData(bookTypes);
		return page;
	}
	
	@Override
	public int addBook(Book book) {
		// TODO Auto-generated method stub
		Book _book = bookDao.checkBookName(book.getBookName());
		if(_book == null) {
			return bookDao.addBook(book);
		}else {
			return -1;
		}
		
	}
	@Override
	public int updateBook(Book book) {
		// TODO Auto-generated method stub
		return bookDao.updateBook(book);
	}
	@Override
	public int deleteBook(int bookId) {
		// TODO Auto-generated method stub
		return bookDao.deleteBook(bookId);
	}
	/**
	 * 借阅书籍
	 */
	@Override
	public boolean borrowBook(User user, int bookId) {
		//获取数据库的连接对象
		// TODO Auto-generated method stub
		//根据bookId获取到完整的book信息
		Book book = bookDao.seleBooksById(bookId);
		//创建图书借阅记录
		History history =  new History();
		//设置数据
		history.setBookId(bookId);
		history.setBookName(book.getBookName());
		history.setUserId(user.getId());
		history.setUserName(user.getUserName());
		history.setCard(book.getCard());;
		history.setNickname(user.getNickname());
		Date begindate = new Date();
		Date endDate =  DateUtils.dateAdd(begindate, user.getLendDayNum());
		history.setBeginTime(new Timestamp(begindate.getTime()));
		history.setEndTime(new Timestamp(endDate.getTime()));
		history.setStatus(0);
		//开启事务
		//获取连接
		Connection connection =  BaseDao.getConnection();
		boolean result = false;
		//关闭事务自动提交
		try {
			connection.setAutoCommit(false);
			
			result = new BookAndUserDaoImpl().borrowBook(user, book, history,connection);
			if(result == true) {
				System.out.println(connection);
				System.out.println("我要提交事务了");
				connection.commit();
			}
			return result;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			//失败就回滚事务
			try {
				System.out.println("我要回滚事务了");
				connection.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			e.printStackTrace();
		}finally {
			try {
				connection.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return result;
		
	}
	@Override
	public PageTool<Book> page(String currentPage, String pageSize) {
		// TODO Auto-generated method stub
		//查询总记录数据
		int count = bookDao.selectBookCount();
		//初始化分页工具
		PageTool<Book> pageTool = new PageTool<Book>(count,currentPage,pageSize);
		List<Book> books =  bookDao.selectPageBook(pageTool.getStartIndex(),pageTool.getPageSize());
		pageTool.setRows(books);
		return pageTool;
	}
	public PageTool<Book> page(String currentPage, String pageSize,String word) {
		// TODO Auto-generated method stub
		//查询总记录数据
		int count = bookDao.selectBookCount(word);
		//初始化分页工具
		PageTool<Book> pageTool = new PageTool<Book>(count,currentPage,pageSize);
		List<Book> books = bookDao.selelectBookPageByCondition(pageTool.getStartIndex(),pageTool.getPageSize(),word);
		pageTool.setRows(books);
		return pageTool;
	}
	@Override
	public Book selectBookByName(String bookName) {
		// TODO Auto-generated method stub
		return bookDao.checkBookName(bookName);
	}
	@Override
	public Book selectBookById(int bookId) {
		// TODO Auto-generated method stub
		return bookDao.seleBooksById(bookId);
	}
	@Override
	public PageTool<Book> selectBooksOrderByTimesDesc(String currentPage, String pageSize) {
		// TODO Auto-generated method stub
		//查询总记录数据
		int count = bookDao.selectBookCount();
		//初始化分页工具
		PageTool<Book> pageTool = new PageTool<Book>(count,currentPage,pageSize);
		List<Book> books = bookDao.selectBooksOrderByDesc(pageTool.getStartIndex(),pageTool.getPageSize());
		pageTool.setRows(books);
		return pageTool;
	}
	@Override
	public Book selectByCard(int card) {
		// TODO Auto-generated method stub
		return bookDao.selectByCard(card);
	}

}
